<?php

namespace App\Http\Controllers\Api;

use App\Model\Cart;
use App\Model\Orders;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class OrdersController extends Controller
{
    //订单信息接口
    public function index(Request $request){
        //每页条数
        $num = 10;
        $user_id = $request->input("user_id");
        $status = $request->input("currentTab");
        $page = $request->input("page");
        $orders = new Orders();
        if(empty($user_id)){
            $this->error('登录状态异常');
        }
        $ordersInfo = $orders->select('orders.real_price','orders.type','orders.id','orders.orders_num','a.num','b.name','b.thumb_img','b.reprice as price','b.id as gid','a.orders_id')
            ->leftJoin('orders_info as a','orders.id','=','a.orders_id')
            ->leftJoin('goods as b','a.goods_id','=','b.id')
            ->where('orders.user_id',$user_id)
            ->where('orders.status',$status)
            ->groupBy('orders.id')
//            ->skip($page*$num)
//            ->take($num)
            ->get();
        if(count($ordersInfo)>0){
            $this->success(array('count'=>count($ordersInfo),'ordersInfo'=>$ordersInfo));
        }else{
            $this->error('暂无数据');
        }

    }
    
    //获取相应状态的订单数量
    public function getOrdersNum(Request $request){
        $user_id = $request->input("user_id");
        $orders = new Orders();
        if(empty($user_id)){
            $this->error('参数错误');
        }
        $ordersInfo[0] = $orders->select(DB::raw('count(*) as num'))->where('user_id',$user_id)->where('status',0)->first()->num;
        $ordersInfo[1] = $orders->select(DB::raw('count(*) as num'))->where('user_id',$user_id)->where('status',1)->first()->num;
        $ordersInfo[2] = $orders->select(DB::raw('count(*) as num'))->where('user_id',$user_id)->where('status',2)->first()->num;
        $ordersInfo[3] = $orders->select(DB::raw('count(*) as num'))->where('user_id',$user_id)->where('status',3)->first()->num;
        $this->success($ordersInfo);
    }

    //购物车结算接口
    public function buyCart(Request $request){
        $userId = $request->input("userId");
        $cartId = $request->input("cartId");
        if(empty($userId) || empty($cartId)){
            $this->error("参数错误");
        }
        $cart = new Cart();
        $id = explode(',',$cartId);
        $count = 0;
        $cartInfo = array();
        foreach ($id as $k=>$v){
            if(!empty($v)){
                $res= $cart->select('cart.num','cart.price','b.name','b.thumb_img')->leftJoin('goods as b','cart.goods_id','=','b.id')->where('cart.id',$v)->where('b.status','1')->first();
                if(count($res)>0){
                    $count += $res['price'] * $res['num'];
                    $cartInfo[] = $res;
                }else{
                    $this->error('非法请求');
                }
            }
        }
        $this->success(array('count'=>$count,'cartInfo'=>$cartInfo));
    }

    
}
